<?php

namespace app\common\library;

use app\common\exception\UploadException;
use app\admin\model\SubjectAttachment;
use Exception;
use fast\Random;
use FilesystemIterator;
use think\Config;
use think\File;
use think\Hook;
use think\Log;

/**
 * 网体统计类
 */
class TeamCount
{
    /**
     * 获取某个用户向下的树形关系链
     */
    public static function getTreeRelations($uids, &$users_array)
    {
        $expert_position_list = db("expert_position")->whereIn("id", $uids)->select();
        if ($expert_position_list) {
            // exit(print_r($expert_position_list));

            $found_user_ids = [];
            foreach ($expert_position_list as $position) {

                for ($i = 1; $i <= 8; $i++) {
                    if ($position["p" . $i] > 0) {
                        $found_user_ids[] = $position["p" . $i];
                    }
                }
            }

            if ($found_user_ids) {
                $users_array = array_merge($users_array, $found_user_ids);
                // exit(print_r($found_user_ids));
                return self::getTreeRelations($found_user_ids, $users_array);
            }
        }
        return $users_array;
    }

    /**
     * 获取某个用户向下的树形关系链(返回用户的达人级别)
     */
    public static function getTreeExpertLevelRelations($uids, &$users_array)
    {
        $expert_position_list = db("expert_position")->whereIn("id", $uids)->select();
        if ($expert_position_list) {
            // exit(print_r($expert_position_list));

            $found_user_ids = [];
            foreach ($expert_position_list as $position) {

                for ($i = 1; $i <= 8; $i++) {
                    if ($position["p" . $i] > 0) {
                        $found_user_ids[] = $position["p" . $i];
                    }
                }
            }

            if ($found_user_ids) {
                $user_list = db("user")->whereIn("id", $found_user_ids)->field("id,expert_level,is_area_manager")->select();
                $users_array = array_merge($users_array, $user_list);
                // exit(print_r($found_user_ids));
                return self::getTreeExpertLevelRelations($found_user_ids, $users_array);
            }
        }
        return $users_array;
    }
}
